/**********************************************************************
* <pre>
* FILE : ServiceActivityPlanAnalyseDao.java
* CLASS : ServiceActivityPlanAnalyseDao
*
* AUTHOR : PGM
*
* FUNCTION :  服务活动管理---服务活动计划分析
*
*
*======================================================================
* CHANGE HISTORY LOG
*----------------------------------------------------------------------
* MOD. NO.| DATE     | NAME | REASON | CHANGE REQ.
*----------------------------------------------------------------------
*         |2010-06-13| PGM  | Created |
* DESCRIPTION:
* </pre>
***********************************************************************/
/**
 * $Id: ServiceActivityManageSummaryDao.java,v 1.1 2010/08/16 01:42:18 yuch Exp $
 */
package com.infodms.dms.dao.claim.serviceActivity;
import java.sql.ResultSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;

import com.infodms.dms.bean.TtAsActivityBean;
import com.infodms.dms.common.Constant;
import com.infodms.dms.dao.common.BaseDao;
import com.infodms.dms.po.TtAsActivityDealerPO;
import com.infodms.dms.po.TtAsActivityEvaluatePO;
import com.infoservice.po3.bean.PO;
import com.infoservice.po3.bean.PageResult;
/**
 * Function       :  服务活动管理---服务活动评估
 * @author        :  PGM
 * CreateDate     :  2010-06-13
 * @version       :  0.1
 */
@SuppressWarnings("unchecked")
public class ServiceActivityManageSummaryDao extends BaseDao{
	public static Logger logger = Logger.getLogger(ServiceActivityManageSummaryDao.class);
	private static final ServiceActivityManageSummaryDao dao = new ServiceActivityManageSummaryDao ();
	public  static final ServiceActivityManageSummaryDao getInstance() {
		return dao;
	}

	/**
	 * Function         : 服务活动管理---服务活动评估
	 * @param           : 活动编号
	 * @param           : 活动开始日期
	 * @param           : 活动结束日期
	 * @param           : 当前页码
	 * @param           : 每页显示记录数
	 * @return          : 满足条件的服务活动管理---服务活动评估信息，包含分页信息
	 * @throws          : Exception
	 * LastUpdate       : 服务活动管理
	 */
	public  PageResult<Map<String, Object>>  getServiceActivityManageSummaryQuery(TtAsActivityBean ActivityBean, int curPage,int pageSize) throws Exception {
		StringBuffer sql = new StringBuffer();
		sql.append("	  SELECT A.ACTIVITY_ID, A.ACTIVITY_CODE, A.ACTIVITY_NAME, TO_CHAR(A.STARTDATE, 'yyyy-MM-dd') AS STARTDATE, TO_CHAR(A.ENDDATE, 'yyyy-MM-dd') AS ENDDATE ,COUNT(C.VIN) as IN_AMOUNT \n");
		sql.append("      FROM TT_AS_ACTIVITY  A, TT_AS_ACTIVITY_DEALER B, TP_AS_SERVICE_VEHICLE C,  TM_DEALER   D     \n");//活动状态为：已经发布;删除标记为：逻辑未删除
		sql.append("      WHERE A.ACTIVITY_ID = B.ACTIVITY_ID AND C.ACTIVITY_CODE = A.ACTIVITY_CODE AND B.dealer_id = D.dealer_id AND TRIM(C.OPERATE_SST_CODE) = TRIM(D.DEALER_CODE)    \n");//活动状态为：已经发布;删除标记为：逻辑未删除
		sql.append("      AND A.STATUS='"+Constant.SERVICEACTIVITY_STATUS_02+"' AND A.IS_DEL='"+Constant.IS_DEL_00+"'   \n");//活动状态为：已经发布;删除标记为：逻辑未删除
		sql.append("      AND B.STATUS='"+Constant.SUMMARY_STATUS_02+"' AND B.dealer_id ="+ActivityBean.getDealerId()+" \n");//服务活动评估状态为：已评估;未评估;
		if(!"".equals(ActivityBean.getCompanyId())&&null!=ActivityBean.getCompanyId()){//公司ID不为空
			sql.append("		AND A.COMPANY_ID = "+ActivityBean.getCompanyId()+"   \n");
		}
		if(!"".equals(ActivityBean.getActivityCode())&&null!=ActivityBean.getActivityCode()){//活动编号不为空
			sql.append("		AND A.ACTIVITY_CODE like '% "+ActivityBean.getActivityCode()+" %'  \n");
		}
		if(!"".equals(ActivityBean.getStartdate())&&!(null==ActivityBean.getStartdate())){      //活动开始日期不为空
			sql.append("		AND A.STARTDATE >=to_date('"+ActivityBean.getStartdate()+" 00:00:00', 'yyyy-MM-dd HH24:mi:ss') \n");
		}
		if(!"".equals(ActivityBean.getEnddate())&&!(null==ActivityBean.getEnddate())){         //活动结束日期不为空
			sql.append("	    AND A.ENDDATE  <= to_date('"+ActivityBean.getEnddate()+" 23:59:59', 'yyyy-MM-dd HH24:mi:ss') \n");
		}
		sql.append("           GROUP BY A.ACTIVITY_ID,  A.ACTIVITY_CODE,  A.ACTIVITY_NAME,  STARTDATE,  ENDDATE  \n");
		PageResult<Map<String, Object>> ps = (PageResult<Map<String, Object>>) pageQuery(sql.toString(), null,  getFunName(), pageSize, curPage);
		return ps;
	}

	@Override
	protected PO wrapperPO(ResultSet rs, int idx) {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * Function         : 服务活动管理---服务活动评估[编辑]查询方法：状态为：[已经下发]的活动
	 * @return          : 满足条件的服务活动管理信息
	 * @throws          : 
	 * LastUpdate       : 2010-06-13
	 */
	public TtAsActivityEvaluatePO serviceActivitySummaryQuery(String activityId,String dealerId){
		List<Object> params = new LinkedList();
		StringBuffer sql = new StringBuffer();
		sql.append("	SELECT A.ACTIVITY_NAME,   org.org_name,COUNT(C.VIN) as IN_AMOUNT   	\n");//已经发布
		sql.append("	FROM  tm_dealer dealer, tm_dealer_org_relation org_relation, tm_org org,TT_AS_ACTIVITY A    	\n");
		sql.append("	LEFT OUTER JOIN TT_AS_ACTIVITY_EVALUATE B ON A.ACTIVITY_ID = B.ACTIVITY_ID 	\n");
		sql.append("	LEFT OUTER JOIN TP_AS_SERVICE_VEHICLE c ON a.activity_code=c.activity_code 	\n");
		sql.append("	WHERE A.STATUS="+Constant.SERVICEACTIVITY_STATUS_02+" AND A.IS_DEL="+Constant.IS_DEL_00+" 	\n");
		sql.append("    and  dealer.dealer_id = org_relation.dealer_id    \n");
		sql.append("    and  org_relation.org_id = org.org_id    \n");
		if (activityId!=null&&!("").equals(activityId)){
			sql.append(" AND A.ACTIVITY_ID = ?  \n");
			params.add(activityId);
			}
		if (dealerId!=null&&!("").equals(dealerId)){
			sql.append(" AND dealer.dealer_id  = ?  \n");
			params.add(dealerId);
			}
		sql.append(" group by A.ACTIVITY_NAME,org.org_name \n");
		TtAsActivityEvaluatePO evaluatePO=new TtAsActivityEvaluatePO();
		List list = dao.select(TtAsActivityEvaluatePO.class, sql.toString(), params);
			if (list!=null){
				if (list.size()>0) {
					evaluatePO = (TtAsActivityEvaluatePO) list.get(0);
				}
			}
        return evaluatePO;
	}
	/**
	 * Function         : 服务活动管理---服务活动评估总结上报
	 * @return          : 满足条件的服务活动管理信息
	 * @throws          : 
	 * LastUpdate       : 2010-06-13
	 */
	public void serviceActivitySummaryOption(TtAsActivityEvaluatePO evaluatePOContent){
		dao.insert(evaluatePOContent);
	}
	/**
	 * Function         : 服务活动管理---服务活动评估总结上报更新状态
	 * @return          : 满足条件的服务活动管理信息
	 * @throws          : 
	 * LastUpdate       : 2010-06-13
	 */
	public void serviceActivitySummaryOptionStatus(TtAsActivityDealerPO DealerPO,TtAsActivityDealerPO DealerPOCon){
		dao.update(DealerPO,DealerPOCon);
	}
}